package rest;

/*
 * http://www.vogella.com/articles/REST/article.html
*/	
import java.io.IOException;

import javax.servlet.ServletException;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

@Path("/Hello")
public class RestService {

	/*// This method is called if TEXT_PLAIN is request
	  @GET
	  @Produces(MediaType.TEXT_PLAIN)
	  public String sayPlainTextHello() {
	    return "Hello Jersey";
	  }

	  // This method is called if XML is request
	  @GET
	  @Produces(MediaType.TEXT_XML)
	  public String sayXMLHello() {
	    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
	  }

	  // This method is called if HTML is request
	  @GET
	  @Produces(MediaType.TEXT_HTML)
	  public String sayHtmlHello() {
	    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
	        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
	  }*/
	  
	private static boolean status = false;
	
	 @GET
	  @Path("/Details/{name}/{number}")
	  @Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)
	  	public String collectDetails(@PathParam("name") String name,@PathParam("number") long number ){
	  		
	  		String firstName = name;
	  		long phoneNumber = number;
	  //		System.out.println("\n firstName recevd in collectDetails() >>> "+firstName);
	  //		System.out.println("\n phoneNumber recevd in collectDetails() >>> "+phoneNumber);
	  		try {
				new DBOperations().addDetails_NP(firstName, phoneNumber);
			} catch (ServletException e) {			
				e.printStackTrace();
			} catch (IOException e) {				
				e.printStackTrace();
			}
	  		
	  		return "Hello "+firstName +" \n Your PhoneNumber is :" +phoneNumber;
	  	}
	 
	 @GET
	  @Path("/add/{number1}/{number2}")
	  @Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)
	  	public String collectDetails3(@PathParam("number1") int a ,@PathParam("number2") int b ){
	  		
	  		int sum =a+b;
	  		return "sum = "+sum;
	  	}

	 @POST
	  @Path("/add2/{number1}/{number2}")
	  @Consumes(javax.ws.rs.core.MediaType.TEXT_PLAIN)
	  	public String collectDetails34(@PathParam("number1") int a ,@PathParam("number2") int b ){
	  		
			System.out.println("inside add2 >>>");

	  		int sum =a+b;
	  		System.out.println(" sum >>>> "+sum);
	  		return "sum = "+sum;
	  	}
	 
	 
	 
	@POST
	@Path("/JSONRequest")
	@Consumes(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	 public String receiveJSONobject_Testing(String requestParam){
		/* ## http://localhost:4040/RestProject/rest/hello/JSONRequest/request*/
		
		String name = "";
		String phone = "";
		String email = "";
		
		//## here the requisite jar(json-simple-1.1.1.jar) for JsonParser is included in the jersey bundle itself.. 
		JSONParser jsonParser = new JSONParser();
		
		// ## Included (json-lib-2.2.jar) for using JsonObject ,etc..
		JSONObject jsonObj = new JSONObject();
		try {
			jsonObj = (JSONObject) jsonParser.parse(requestParam);
			name = jsonObj.get("name").toString();
			phone = jsonObj.get("phone").toString();
			email = jsonObj.get("email").toString();
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		String response = ("name +phone + email recevd in RestServicesExample2 >>> "+name +" >> "+phone +" >> "+ email);
		 
		 return response ; 
	 }
	 
	
	
	 
	 /*
	 * USER-REGISTRATION 
	 */ 
	 @GET
	  @Path("/userRegistration/{name1}/{name2}/{name3}/{name4}")
	  @Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)
	  	public String userRegistrationDetails(@PathParam("name1") String name,@PathParam("name2") String phoneNumber ,@PathParam("name3") String email ,@PathParam("name4") String password ){
	  		
		 /*http://localhost:4040/RestProject/rest/hello/ProcessBooking/aaa/bbb/ccc/ddd/eeee/ffff*/		
		
		 String firstName = name;
	  		System.out.println("\n firstName recevd in collectDetails() >>> "+firstName);
	  		System.out.println("\n phoneNumber recevd in collectDetails() >>> "+phoneNumber);
	  		try {
	  		 status = new DBOperations().doUserRegistration_NPE(name, phoneNumber, email, password);
			
	  		}catch(Exception e){
				e.printStackTrace();
			}
	  		
	  		return "Hello "+firstName +" \n you are registered with this PhoneNumber is :" +phoneNumber +" your status >>"+status;
	  	}

	 
	 /*
	 * Bookings-REGISTRATION 
	 */
		@GET
		@Path("/ProcessBookingWithPlanType/{name_}/{phone_}/{email_}/{pickUp_}/{destination_}/{pickUpDate_}/{pickUpTime_}/{planType_}")
		@Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)
		public String registerBookingDetailsWithPlanType(@PathParam("name_") String name,@PathParam("phone_")String phone,@PathParam("email_")String email,
				@PathParam("pickUp_")String pickUp,@PathParam("destination_")String destination,@PathParam("pickUpDate_")String pickUpDate,
				@PathParam("pickUpTime_")String pickUpTime,@PathParam("planType_")String planType){

			/*http://localhost:8080/RestProject/rest/hello/ProcessBookingWithPlanType/aaa/bbb/ccc/ddd/eeee/ffff/ggg*/	

			String response="";

			System.out.println("\n name recevd in submitBookingDetailsWithPlanType() >>> "+name);
			System.out.println("\n phone recevd in submitBookingDetailsWithPlanType() >>> "+phone);
			System.out.println("\n email recevd in submitBookingDetailsWithPlanType() >>> "+email);
			System.out.println("\n pickUp recevd in submitBookingDetailsWithPlanType() >>> "+pickUp);
			System.out.println("\n destination recevd in submitBookingDetailsWithPlanType() >>> "+destination);
			System.out.println("\n travelDate recevd in submitBookingDetailsWithPlanType() >>> "+pickUpDate);
			System.out.println("\n travelDate recevd in submitBookingDetailsWithPlanType() >>> "+pickUpTime);
			System.out.println("\n planType recevd in submitBookingDetailsWithPlanType() >>> "+planType);

			try {
			 status = new DBOperations().doProcessBooking(name, phone, email, pickUp, destination, pickUpDate, pickUpTime, planType);

			}catch(Exception e){
				e.printStackTrace();
			}

			response = "Hello "+name +"\n Your PhoneNumber is :" +phone +"\n Your email is :" +email +"\n Your pickUp is :" +pickUp +"\n Your destination is :" +destination
					+"\n Your pickUpDate is :" +pickUpDate+"\n Your pickUpTime is :" +pickUpTime+"\n Your planType is :" +planType+"\n\n"+" --STATUS-- ::"+status;

			return response;
		}
		
		
}
